package cn.edu.scau.cmi.maintain.controller;

import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.domain.Site;
import cn.edu.scau.cmi.front.controller.FrontCmiEmaController;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Set;

/**
 * 原来是在测试源文件夹中的方法，简化起见，放到
 * 
 */

@Controller()

//@PropertySource(value = { "classpath:food2type.properties" })

public class MaintainSiteController extends FrontCmiEmaController {
	
	@Transactional(timeout = 300000000)
	@RequestMapping(value = "/constructSites", method = RequestMethod.GET)
	public void constructSites() throws Exception {
		
		Set<Address> provinces = addressDAO.findAddressByLevel(3);
		
		Address china = addressDAO.findAddressById(9);
		Set<Address> chinaProvinces = china.getAddresssForParent();
		
		for(Address chinaProvince : chinaProvinces) {
			constructSite(chinaProvince);
			
			Set<Address> cities = chinaProvince.getAddresssForParent();
			
			for(Address city : cities) {
				constructSite(city);
			}
		}
	}

	private void constructSite(Address address) {
		String addressName = address.getName();
		Site site = new Site();
		site.setName(addressName  + "市场监督管理局");
		site.setDomain("请查找" + addressName + "的市场监督管理局的网站地址确认" );
		site.setDescription(addressName + "的市场监督管理局的网址还没有确认，请查找确认！！！"); 
		System.out.println(addressName + "的site已经插入数据库");
		siteService.saveSite(site);
		
	}
	
}